package scantask.containers;

import scantask.containers.exceptions.InvalidWeekNumberException;
import scantask.containers.exceptions.InvalidYearException;

public class WeekDate implements Comparable<WeekDate> {
	
	private final static int NUMBER_WEEKS_IN_YEAR = 52;
	
	private int _week;
	private int _year;
	
	public WeekDate(int week, int year) throws InvalidWeekNumberException, InvalidYearException {
		if ((week < 0) || (week > NUMBER_WEEKS_IN_YEAR)) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new WeekDate illegale week");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new WeekDate illegale week");
			throw new InvalidWeekNumberException();
		}
		if (year < 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new WeekDate illegale year");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new WeekDate illegale year");
			throw new InvalidYearException();
		}
		
		this._week = week;
		this._year = year;
	}
	
	public int getWeek() {
		return this._week;
	}
	
	public int getYear() {
		return this._year;
	}
	
	public void setWeek(int week) throws InvalidWeekNumberException {
		if ((week < 0) || (week > NUMBER_WEEKS_IN_YEAR)) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed set week, illegale week");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed set week, illegale week");
			throw new InvalidWeekNumberException();
		}
		
		this._week = week;
	}
	
	public void setYear(int year) throws InvalidYearException {
		if (year < 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed set year, illegale year");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed set year, illegale year");
			throw new InvalidYearException();
		}
		
		this._year = year;
	}
	
	
	public boolean equals(Object other) {
		if ((other == null) || (!(other instanceof WeekDate))) {
			return false;
		}
		WeekDate date = (WeekDate)other; 
		if ((this._week == date.getWeek()) && (this._year == date.getYear())) {
			return true;
		}
		return false;
	}

	@Override
	public int compareTo(WeekDate other) {
		if (other == null) {
			return 1;
		}
		if (this._year < other.getYear()) {
			return -1;
		}
		if (this._year > other.getYear()) {
			return 1;
		}
		if (this._year == other.getYear()) {
			if (this._week < other.getWeek()) {
				return -1;
			}
			if (this._week > other.getWeek()) {
				return 1;
			}
		}
		return 0;
	}
	
	

}
